package producter

import (
	"github.com/labstack/echo"
	"shop_be/model"
	"strconv"
)

func ProducterRouter(app *echo.Echo) {

	app.POST("/producter/create", func(c echo.Context) error {

		name := c.FormValue("name")
		price := c.FormValue("price")
		totalcount := c.FormValue("totalcount")
		intro := c.FormValue("intro")
		url := c.FormValue("url")
		p := model.Product{}
		p.Name = name
		price2, _ := strconv.ParseFloat(price, 64)
		totalcount2, _ := strconv.Atoi(totalcount)
		p.Price = float32(price2)
		p.TotalCount = totalcount2
		p.Count=totalcount2
		p.Url = url
		p.Introduce = intro
		msg := createProducter(p)

		return c.JSON(200, msg)
	})
	app.POST("/producter/upload", func(c echo.Context) error {

		file, err := c.FormFile("file")
		if err != nil {

			return err

		}
		msg := upLoad(file)

		return c.JSON(200, msg)
	})
	app.POST("/producter/delete", func(c echo.Context) error {
		id := c.FormValue("id")
		msg := deleteProduct(id)
		return c.JSON(200, msg)
	})
	app.POST("/producter/update", func(c echo.Context) error {
		book := c.FormValue("producter")
		msg := updateProduct(book)
		return c.JSON(200, msg)
	})
	//app.POST("/producter/damage", func(c echo.Context) error {
	//	id := c.FormValue("id")
	//	msg := bookDamage(id)
	//	return c.JSON(200, msg)
	//})
	app.GET("/producter/find/:id", func(c echo.Context) error {
		id := c.Param("id")
		msg := findProductById(id)
		return c.JSON(200, msg)
	})
	app.GET("/producter/find/:name", func(c echo.Context) error {
		name := c.Param("name")
		msg := findBookByName(name)
		return c.JSON(200, msg)
	})

	app.GET("/producter/activity", func(c echo.Context) error {
		msg := activity()
		return c.JSON(200, msg)
	})

	app.GET("/producter/get", func(c echo.Context) error {
		msg := getProduct()
		return c.JSON(200, msg)
	})

}
